package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author ：libowen
 * @date ：Created in 2020/1/3
 * @description ：菜单控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {


    @Reference
    private MenuService menuService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result  findPage(QueryPageBean queryPageBean){
    //rpc 分页查询
    PageResult pageResult = menuService.findPage(queryPageBean);

    return new Result(true,MessageConst.GET_MENU_SUCCESS,pageResult);
    }

    /**
     * 更新
     * @param menu
     * @return
     */
    @PostMapping("/edit")
    @PreAuthorize("hasAnyAuthority('MENU_EDIT')")
    public Result  edit(@RequestBody Menu menu){
        log.info("更新菜单data:{}",menu);
        //rpc 远程调用更新
        menuService.edit(menu);

        return new Result(true,"更新菜单成功");
    }

    /**
     * 添加
     * @param menu
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    public Result  add(@RequestBody Menu menu){
        log.info("添加菜单data:{}",menu);

        menuService.add(menu);
        return new Result(true,"添加菜单成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public  Result  findById(Integer id){
       Menu menu =  menuService.findById(id);
       return  new Result(true,"获取菜单成功",menu);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @PostMapping("/delById")
    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")
    public Result delById(Integer id){

       menuService.delById(id);

       return new Result(true,"删除成功");
    }

    /**
     * 查询所有父级菜单
     * @return
     */
    @GetMapping("/findParentMenu")
    public Result findParentMenu(){
      List<Menu> parentMenus =   menuService.findParentMenu();
      return new Result(true,"获取父级菜单成功",parentMenus);
    }
}
